//
//  LCWUserAccountTools.swift
//  MyWB
//
//  Created by 李成万 on 16/6/26.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

class LCWUserAccountTools: NSObject {

    static let sharedTool : LCWUserAccountTools = LCWUserAccountTools()
    
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
    
    var userAccount : LCWUserAccount?
    
    override init() {
        super.init()
        userAccount = geiUserAccount()
    }
    
    var accessToken:String?{
//        print(userAccount?.access_token )
        if userAccount?.access_token == nil{
            return nil
        }else{
            if userAccount?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token
            }else{
                return nil
            }
        }
    }
    
    var isLogin : Bool{
        
        return accessToken != nil
    }
    
    func setUserAccountInfo(userAccount:LCWUserAccount){
        self.userAccount = userAccount
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
        
    }
    func geiUserAccount() -> LCWUserAccount?{
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LCWUserAccount
        return result
    }
}
